
using KinonekoSoftware.CodariaDB.Documents.Index;
namespace KinonekoSoftware.CodariaDB.Documents.Templates
{
    public sealed partial class TemplateEngine : Engine
    {

        protected override void OnDatabaseOpening(RepositoryOpenContext context)
        {
            var repo                = context.Repository;
            var hasInstanceTemplate = repo.Has(CollectionNames.Template_InstanceDB);

            InstanceTemplateDB = repo.Get<InstanceTemplate>(CollectionNames.Template_InstanceDB);
            ModuleCacheDB      = repo.Get<ModuleTemplateCache>(CollectionNames.Template_ModuleDB);
            ModuleTemplateDB   = repo.Get<ModuleTemplate>(CollectionNames.Template_ModuleDB);
            
            if (!hasInstanceTemplate)
            {
                InitializeInstanceTemplates();
            }
        }

        protected override void OnDatabaseClosing(ILogger logger)
        {
            ModuleCacheDB      = null;
            ModuleTemplateDB   = null;
            InstanceTemplateDB = null;
        }

        public ILiteCollection<ModuleTemplate>      ModuleTemplateDB   { get; private set; }
        public ILiteCollection<ModuleTemplateCache> ModuleCacheDB      { get; private set; }
        public ILiteCollection<InstanceTemplate> InstanceTemplateDB { get; private set; }

        public override string EngineID => EngineNames.Template;
    }
}